import { message } from "antd";
import { useContext, createContext, type PropsWithChildren } from "react";
import type { MessageInstance } from "antd/es/message/interface";

const messageContext = createContext({} as MessageInstance);

export function useMessage() {
  return useContext(messageContext);
}

export default function AntdMessageProvider({ children }: PropsWithChildren) {
  const [api, context] = message.useMessage();
  return (
    <>
      {context}
      <messageContext.Provider value={api}>{children}</messageContext.Provider>
    </>
  );
}
